<template>
  <div>
    <div class="top">
      <div style="float: right;margin-right: 10px;">
        <el-input
          v-model="kwInput"
          placeholder="输入交易号或者事件ID 进行筛选"
          class="input-with-select"
          clearable
          @keyup.native.enter="handleSearch"
        >
          <el-button slot="append" icon="el-icon-search" @click="handleSearch">搜索</el-button>
        </el-input>
      </div>
      <div style="float: right;margin-right: 10px;">
        <el-select v-model="dispute_state" placeholder="状态" @change="handleSearch">
          <el-option label="需要处理" value="REQUIRED_ACTION" />
          <el-option label="所有" value="" />
        </el-select>
      </div>
      <el-button type="text" icon="el-icon-plus" @click="handleAddConfig">配置管理</el-button>
      <div style="clear: both" />
    </div>

    <el-table
      v-loading.fullscreen.lock="listLoading"
      :data="data"
      border
      fit
      highlight-current-row
      style="width: 100%"
      @sort-change="handleSort"
    >
      <el-table-column align="center" width="180" prop="dispute_id" label="争议编号" />
      <el-table-column align="center" width="180" prop="transaction_id" label="交易编号" />
      <el-table-column align="center" width="220" prop="account" label="账号" />
      <el-table-column align="center" width="120" prop="dispute_amount" label="金额">
        <template slot-scope="scope">
          <span>{{ scope.row.dispute_currency }}{{ scope.row.dispute_amount }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" width="250" prop="dispute_state" label="争议状态">
        <template slot-scope="scope">
          <span>{{ scope.row.dispute_state }}</span>
          <el-popover
            placement="right"
            width="60%"
            trigger="hover"
          >
            <div>
              <ul>
                <li>争议原因: {{ scope.row.reason }}</li>
              </ul>
            </div>
            <i slot="reference" style="float: right" class="el-icon-view" title="查看详情" />
          </el-popover>
          <!--          <span @click="handleDrawer(scope.row)">{{scope.row.subject}}</span>-->
        </template>
      </el-table-column>
      <el-table-column align="center" width="180" prop="seller_response_due_date" label="最晚处理" />
      <el-table-column align="center" width="280" prop="domain" label="订单网站">
        <template slot-scope="scope">
          <span title="查询订单信息" @click="handleSearchOrderInfo(scope.row.reference)">{{ scope.row.domain }}</span>
          <el-popover
            placement="right"
            width="60%"
            trigger="hover"
          >
            <div>
              <ul>
                <li>系统单号: {{ scope.row.psorder_id }}</li>
                <li>当前状态: {{ scope.row.state_name }}</li>
              </ul>
            </div>
            <i slot="reference" style="float: right" class="el-icon-view" title="查看详情" />
          </el-popover>
          <!--          <span @click="handleDrawer(scope.row)">{{scope.row.subject}}</span>-->
        </template>
      </el-table-column>
      <el-table-column align="center" width="100" prop="finished" label="是否结束">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="点击调整状态" placement="bottom-start">
            <el-switch v-model="scope.row.finished" @change="handleModify(scope.row)" />
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="success" @click="handleShowMessages(scope.row)">回复</el-button>
          <el-button size="mini" type="primary" @click="handleSync(scope.row)">同步</el-button>
        </template>
      </el-table-column>

    </el-table>
    <el-pagination
      v-if="data.length"
      background
      class="mt-pagination"
      :current-page="page"
      :page-sizes="[10,20,50,100]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handlePageChange"
    />
    <el-dialog
      :visible.sync="messageDialogVisible"
      append-to-body
      :title="innertitle"
      :close-on-click-modal="false"
    >
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in messages"
          :key="index"
          :color=" activity.posted_by==='SELLER' ? '#0bbd87': 'red'"
          :timestamp="activity.time_posted"
          placement="top"
        >
          <el-card>
            <p>{{ activity.content }}</p>
            <p>{{ activity.posted_by }} 提交于 {{ activity.time_posted }}</p>
          </el-card>
        </el-timeline-item>
      </el-timeline>
      <el-form :model="temp" label-position="left" label-width="120px">

        <el-form-item v-if="action !== 'delete'" label="回复内容">
          <el-input v-model="temp.message" type="textarea" :rows="4" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="messageDialogVisible = false">取 消</el-button>
        <el-button @click="handleSendMessage">确 定</el-button>
      </span>
    </el-dialog>
    <!--    <el-dialog-->
    <!--      append-to-body-->
    <!--      :title='innertitle'-->
    <!--      :width=width-->
    <!--      :visible.sync=innerDialogVisible-->
    <!--      :close-on-click-modal="false">-->
    <!--      <span style="display: block;margin-bottom: 10px">客户消息: {{temp.customer_message}}</span>-->
    <!--      <el-form :model="temp" label-position="left" label-width="120px">-->

    <!--        <el-form-item label="回复内容" v-if="action !== 'delete'">-->
    <!--          <el-input type="textarea" :rows="4" v-model="temp.message"></el-input>-->
    <!--        </el-form-item>-->
    <!--      </el-form>-->

    <!--      <span slot="footer" class="dialog-footer">-->
    <!--            <el-button @click="innerDialogVisible = false">取 消</el-button>-->
    <!--            <el-button :type="action !== 'delete'?'primary':'danger'" @click="handleSendMessage">确 定</el-button>-->
    <!--          </span>-->
    <!--    </el-dialog>-->
    <paypalconfig :account="account" :dialog-visible="paypalconfigdialogVisible" @close="paypalconfigdialogVisible=false" />
    <orderinfo :keyword="orderinfokeyword" :orderinfo-visible="orderinfoVisible" @closeDialog="orderinfoVisible=false" />
  </div>
</template>

<script>
import { Get, Put, SendMessage, Sync } from '@/api/eco/paypaldispute'
import qs from 'qs'
import Paypalconfig from './element/paypalconfig'
import Orderinfo from '../orders/element/orderinfo'

const orderMapper = {
  'ascending': 0,
  'descending': 1,
  'null': ''
}
export default {
  name: 'Paypaldispute',
  components: { Paypalconfig, Orderinfo },
  data() {
    return {
      orderinfoVisible: false,
      orderinfokeyword: '',
      account: '',
      dispute_state: 'REQUIRED_ACTION',
      paypalconfigdialogVisible: false,
      drawer: false,
      data: [],
      messageDialogVisible: false,
      messages: [],
      listLoading: false,
      kwInput: '',
      finished: 'false',
      order_by: 'seller_response_due_date',
      order_desc: 0,
      // pagination
      pagesize: 20,
      page: 1,
      total: 0,
      action: null,
      innertitle: '回复消息',
      temp: {
        customer_message: '',
        message: '',
        id: ''
      },
      width: '',
      innerDialogVisible: false
    }
  },
  created() {
    this.handleSearch()
  },
  methods: {
    handleSearchOrderInfo(keyword) {
      this.orderinfokeyword = keyword
      this.orderinfoVisible = true
      console.log(keyword)
    },
    handleSync(row) {
      Sync(row.id
      ).then(response => {
        this.notify('同步成功')
        this.getList()
      })
    },
    handleShowMessages(row) {
      this.messages = row.messages
      this.innertitle = '回复争议' + row.dispute_id
      this.temp.id = row.id
      this.temp.customer_message = row.message
      this.temp.message = ''
      this.messageDialogVisible = true
    },
    handleOpenSendMessage(row) {
      console.log(row)
      this.innertitle = '回复争议' + row.dispute_id
      this.temp.id = row.id
      this.temp.customer_message = row.message
      this.temp.message = ''
      console.log(row.message)
      this.innerDialogVisible = true
    },
    handleSendMessage() {
      SendMessage({ 'id': this.temp.id, 'message': this.temp.message }
      ).then(response => {
        this.innerDialogVisible = false
        this.notify('发送成功')
      })
    },
    handleAddConfig() {
      this.paypalconfigdialogVisible = true
    },
    getList() {
      this.listLoading = true
      Get(null, qs.stringify({
        kw: this.kwInput,
        finished: this.finished,
        dispute_state: this.dispute_state,
        pagesize: this.pagesize,
        page: this.page,
        order_by: this.order_by,
        order_desc: this.order_desc
      }))
        .then(response => {
          this.scrollTop()
          this.data = response.data.data
          this.listLoading = false
          this.total = response.data.total
        }).catch(err => {
          console.log(err)
          this.listLoading = false
        })
    },
    handleModify(row) {
      Put(row.id, {
        finished: row.finished
      }).then((response) => {
        this.notify('更新成功')
      })
    },
    notify(message, duration) {
      this.$notify({
        title: '成功',
        message: message,
        type: 'success',
        duration: duration || 2000
      })
    },
    // pageSize 改变后触发 table 更新
    handleSizeChange(val) {
      this.pagesize = val
      this.getList()
    },
    // page 改变后触发 table 更新
    handlePageChange(val) {
      this.page = val
      this.getList()
    },
    // 点击搜索
    handleSearch() {
      this.page = 1
      this.getList()
    },
    handleSort(v) {
      this.order_by = v.prop
      this.order_desc = orderMapper[v.order]
      this.getList()
    },
    scrollTop() {
      window.scrollTo(0, 0)
    },
    close() {
      this.$emit('closeWebDialog', false)
    }
  }
}
</script>
